home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / new_window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  7.8 KB  |  362 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: new_window.c,v 1.1 89/03/17 08:21:17 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/new_window.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/new_window.c,v $$Revision: 1.1 $";
  12.  
  13. /* Create a new window */
  14.  
  15. #include "bitmap.h"
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include "defs.h"
  19. #include "window.h"
  20. #include "font.h"
  21. #include "menu.h"
  22.  
  23. /* sweep out a new window */
  24.  
  25. int new_window()
  26.    {
  27.    register WINDOW *win;
  28.    int dx=16,dy=16;
  29.    char *malloc();
  30.    WINDOW * insert_win();
  31.  
  32.    if (next_window >= MAXWIN)
  33.        return(-1);
  34.    SETMOUSEICON(&mouse_box);
  35.    move_mouse(screen,mouse,&mousex,&mousey,0);
  36.    SETMOUSEICON(&mouse_arrow);
  37.    get_rect(screen,mouse,mousex,mousey,&dx,&dy,0);
  38.    do_button(0);
  39.  
  40.    return( create_window( mousex, mousey, dx, dy, -1, 0 ) );
  41.    }
  42.  
  43. /* insert a new window into the window list */
  44.  
  45. WINDOW *
  46. insert_win(win)
  47. WINDOW *win;
  48.    {
  49.    char *malloc();
  50.  
  51.    if (win == (WINDOW *) 0 &&
  52.            (win = (WINDOW *) malloc(sizeof(WINDOW))) == (WINDOW *) 0) {
  53.       if( debug )
  54.      fprintf(stderr,"Can't malloc window space\n");
  55.       return(win);
  56.       }
  57.  
  58.    if (active) {
  59.       W(prev) = ACTIVE(prev);
  60.       ACTIVE(prev) = win;
  61.       W(next) = active;
  62.       }
  63.    else {
  64.       W(prev) = win;
  65.       W(next) = (WINDOW *) 0;
  66.       }
  67.    return(win);
  68.    }
  69.  
  70. /* create a new window given coords */
  71.  
  72. int
  73. create_window(x,y,dx,dy,font_num,argv)
  74. int x,y,dx,dy;
  75. int font_num;
  76. char **argv;
  77.    {
  78.    register WINDOW * win;
  79.    WINDOW * insert_win();
  80.  
  81.    if (next_window >= MAXWIN)
  82.        return(-1);
  83.    if (check_window(x,y,dx,dy,font_num) == 0)
  84.       return(-1);
  85.  
  86.    /* alloc window space */
  87.  
  88.    if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == (WINDOW *) 0) {
  89.       fprintf(stderr,"Can't malloc window space\n");
  90.       return(-1);
  91.       }
  92.       
  93.    if ((W(pid) = get_command(argv,&W(from_fd))) < 0) {
  94.       free(win);
  95.       fprintf(stderr,"Can't get a pty\n");
  96.       return(-1);
  97.       }
  98.    W(to_fd) = W(from_fd);
  99.    W(setid) = next_windowset_id();
  100.  
  101.    active = insert_win(win);
  102.  
  103.    make_window(screen,x,y,dx,dy,font_num,"");
  104.    return(0);
  105.    }
  106.  
  107. /* create a new window given coords , with only 1/2 a ptty */
  108.  
  109. char *
  110. half_window(x,y,dx,dy,font_num)
  111. int x,y,dx,dy;
  112. int font_num;
  113.    {
  114.    register WINDOW * win;
  115.    WINDOW * insert_win();
  116.    char *half_open();
  117.    char *tty;
  118.  
  119.    if (next_window >= MAXWIN)
  120.        return(NULL);
  121.    if (check_window(x,y,dx,dy,font_num) == 0)
  122.       return(NULL);
  123.  
  124.    /* alloc window space */
  125.  
  126.    if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == (WINDOW *) 0) {
  127.       fprintf(stderr,"Can't malloc window space\n");
  128.       return(NULL);
  129.       }
  130.       
  131.    if ((tty = half_open(&W(from_fd))) == NULL) {
  132.       free(win);
  133.       fprintf(stderr,"Can't get a pty\n");
  134.       return(NULL);
  135.       }
  136.  
  137.    W(setid) = next_windowset_id();
  138.    active = insert_win(win);
  139.  
  140.    W(to_fd) = W(from_fd);
  141.    make_window(screen,x,y,dx,dy,font_num,"");
  142.    W(pid) = 1;
  143.    W(flags) |= W_NOKILL;
  144.  
  145.    return(tty);
  146.    }
  147.  
  148. /* check window size */
  149.  
  150. int
  151. check_window(x,y,dx,dy,fnt)
  152. int x, y, dx, dy;
  153. int fnt;
  154.    {
  155.    struct font *curr_font, *Get_font();
  156.  
  157.    if (dx<0)
  158.       x += dx, dx = -dx;
  159.    if (dy<0)
  160.       y += dy, dy = -dy;
  161.    
  162.    if (x >= BIT_WIDE(screen) || y >= BIT_HIGH(screen))
  163.        return(0);
  164.  
  165.    if (x + dx >= BIT_WIDE(screen))
  166.       dx = BIT_WIDE(screen)-x;
  167.  
  168.    if (y + dy >= BIT_HIGH(screen))
  169.       dy = BIT_HIGH(screen)-y;
  170.  
  171.    curr_font = Get_font(fnt);
  172.  
  173. #ifdef DEBUG
  174.    dprintf(n)(stderr,"starting: (%d,%d)  %d x %d\r\n",x,y,dx,dy);
  175. #endif
  176.  
  177.    if (dx < SUM_BDR + curr_font->head.wide*MIN_X +1 ||
  178.               dy < SUM_BDR + curr_font->head.high*MIN_Y +1)
  179.       return(0);
  180.    else
  181.       return(1);
  182.    }
  183.  
  184. /* draw the window on the screen */
  185.  
  186. make_window(screen,x,y,dx,dy,fnt,start)
  187. BITMAP *screen;
  188. int x, y, dx, dy;
  189. int fnt;
  190. char *start;
  191.    {
  192.    register WINDOW *win = active;
  193.    register int i;
  194.    struct font *curr_font, *Get_font();
  195.    char *last_tty();
  196.  
  197.    if (dx<0)
  198.       x += dx, dx = -dx;
  199.    if (dy<0)
  200.       y += dy, dy = -dy;
  201.    
  202.    if (x < 0) x = 0;
  203.  
  204.    if (x + dx >= BIT_WIDE(screen))
  205.       dx = BIT_WIDE(screen)-x;
  206.  
  207.    if (y + dy >= BIT_HIGH(screen))
  208.       dy = BIT_HIGH(screen)-y;
  209.  
  210.    curr_font = Get_font(fnt);
  211.    if (curr_font == font) {
  212. #ifdef DEBUG
  213.       dprintf(n)(stderr,"Can't find font %d, using default\r\n", fnt);
  214. #endif
  215.       }
  216.  
  217. #ifdef DEBUG
  218.    dprintf(n)(stderr,"starting window: (%d,%d)  %d x %d font (%d,%d)\r\n",
  219.              x,y,dx,dy,curr_font->head.wide, curr_font->head.high);
  220.    dprintf(n)(stderr,"min size: %d x %d\r\n",
  221.            SUM_BDR + curr_font->head.wide*MIN_X +1,
  222.            SUM_BDR + curr_font->head.high*MIN_Y +1);
  223. #endif
  224.  
  225.    if (dx < SUM_BDR + curr_font->head.wide*MIN_X +1 ||
  226.        dy < SUM_BDR + curr_font->head.high*MIN_Y +1)
  227.        return(-1);
  228.  
  229. #ifdef DEBUG
  230.    dprintf(n)(stderr,"adjusted to: (%d,%d)  %d x %d\r\n",x,y,dx,dy);
  231. #endif
  232.  
  233.    if (!setup_window(win,curr_font,x,y,dx,dy)) {
  234.       fprintf(stderr,"Out of memory for window creation -- bye!\n");
  235.       quit();
  236.       }
  237.  
  238.    next_window++;
  239.  
  240.    /* make the window */
  241.  
  242.    set_covered(win);
  243.    border(win,BLK_BDR,WH_BDR);
  244.    CLEAR(W(window),BIT_CLR);
  245.  
  246.    /* set up file descriptor modes */
  247.  
  248.    if (fcntl(W(from_fd),F_SETFL,fcntl(W(from_fd),F_GETFL,0)|FNDELAY) == -1)
  249.       fprintf(stderr,"%s: fcntl failed for fd %d\n",W(tty),W(from_fd));
  250.  
  251.    mask |= 1<<W(to_fd);
  252.  
  253.    /* send initial string (if any) */
  254.  
  255.    if (start && *start) {
  256. #ifdef DEBUG
  257.       dprintf(n)(stderr,"Sending initial string: [%s]\n",start);
  258. #endif
  259.       Write(W(to_fd),start,strlen(start));
  260.       }
  261.    return(0);
  262.    }
  263.  
  264. /* initialize window state */
  265.  
  266. int
  267. setup_window(win,curr_font,x,y,dx,dy)
  268. register WINDOW *win;
  269. int x,y,dx,dy;
  270. struct font *curr_font;
  271.    {
  272.    register int i;
  273.  
  274. #ifdef ALIGN
  275.    alignwin(screen,&x,&dx,SUM_BDR);
  276. #endif
  277.  
  278.    W(font) = curr_font;
  279.    W(x) = 0;
  280.    W(y) = curr_font->head.high;
  281.    W(esc_cnt) = 0;
  282.    W(esc[0]) = 0;
  283.    W(flags) = W_ACTIVE | init_flags;
  284. #ifdef CUT
  285.    W(flags) |= W_SNARFABLE;
  286. #endif
  287. #ifdef COLOR
  288.    W(background) = NOCOLOR&BIT_SRC | GETCOLOR(WHITE);
  289.    W(style) = NOCOLOR&BIT_SRC | GETCOLOR(BLACK);
  290. #else
  291.    W(style) = OPCODE(BIT_SRC);
  292.    W(background) = OPCODE(BIT_CLR);
  293. #endif
  294.     W(curs_type) = CS_BLOCK;
  295.    W(x0) = x;
  296.    W(y0) = y;
  297.    W(border) = bit_create(screen,x,y,dx,dy);
  298.    W(window) = bit_create(W(border),SUM_BDR,SUM_BDR,dx-SUM_BDR*2,dy-SUM_BDR*2);
  299.  
  300.    W(text.x) = 0;
  301.    W(text.y) = 0;
  302.    W(text.wide) = 0;
  303.    W(text.high) = 0;
  304.  
  305.    W(bitmap) = (BITMAP *) 0;
  306.    for(i=0;i<MAXBITMAPS;i++)
  307.       W(bitmaps)[i] = (BITMAP *) 0;
  308.  
  309.    W(save) = (BITMAP *) 0;
  310.    W(stack) = (WINDOW *) 0;
  311.    W(main) = win;
  312.    W(alt) = (WINDOW *) 0;
  313.    W(esc_cnt) = 0;
  314.    W(esc[0])=0;
  315.    W(clip_list) = (char *) 0;
  316.  
  317.    for(i=0;i<MAXMENU;i++)
  318.       W(menus[i]) = (struct menu_state *) 0;
  319.  
  320.    W(menu[0]) = W(menu[1]) = -1;
  321.    W(event_mask) = 0;
  322.  
  323.    for(i=0;i<MAXEVENTS;i++)
  324.       W(events)[i] = (char *) 0;
  325.  
  326.    W(snarf) = (char *) 0;
  327.    W(gx) = 0;
  328.    W(gy) = 0;
  329.    W(op) = OPCODE(BIT_SET);
  330.    W(max) = 0;
  331.    W(current) = 0;
  332.    strcpy(W(tty), last_tty());
  333.    W(num) = 0;
  334.    clip_bad(win);    /* invalidate clip lists */
  335.    return(W(border) && W(window));
  336.    }
  337.  
  338. /*
  339.     Look through all the windows for the next available window set id.
  340. */
  341.  
  342. int
  343. next_windowset_id()
  344.    {
  345.       char        list[ MAXWIN + 2 ];
  346.       register char    *cp;
  347.       register WINDOW    *win;
  348.  
  349.       for( cp = list;  cp < &list[ MAXWIN + 2 ];  cp++ )
  350.      *cp = 0;
  351.  
  352.       for( win = active;  win != (WINDOW *)0;  win = W(next) )
  353.      list[ W(setid) ] = 1;
  354.  
  355.       /*    There is no window set ID zero.
  356.       */
  357.       for( cp = list + 1;  *cp;  cp++ )
  358.      ;
  359.  
  360.       return cp - list;
  361.    }
  362.